import pdfkit
import requests
from bs4 import BeautifulSoup

page = requests.get("https://wizardforcel.gitbooks.io/java8-tutorials/content/index.html")
response = page.content
requestSoup = BeautifulSoup(page.content)
findHrefLiItems = requestSoup.find_all("li", class_="chapter")
allUrlList = []
for item in findHrefLiItems:
    findItem = item.find("a")
    if findItem:
        currentFindItemValue = findItem.get("href")
        if currentFindItemValue and currentFindItemValue != "./index.html":
            allUrlList.append(
                "https://wizardforcel.gitbooks.io/java8-tutorials/content/" + str(currentFindItemValue)[2:])
            # print(findItem.get("href"))


class GetResponse:
    def request(self, url):
        reqPage = requests.get(url)
        reqPageResponse = reqPage.content
        initSoup = BeautifulSoup(reqPageResponse, "html.parser")
        getSoupBody = initSoup.find("div", class_="book-body")
        return str(getSoupBody)


getResponse = GetResponse()
sumSize = 1

saveFileList = []


def save_pdf(htmls):
    """
    把所有html文件转换成pdf文件
    """
    options = {
        'page-size': 'Letter',
        'encoding': "UTF-8",
        'custom-header': [
            ('Accept-Encoding', 'gzip')
        ]
    }

    pdfkit.from_file(htmls, "D://PDF//PDF.pdf", options=options)


for item in allUrlList:
    fileName = "D://PDF//" + str(item).split("/")[-1]
    print("当前第" + str(sumSize) + "正在采集" + fileName)
    bodyTag = str(getResponse.request(item))
    saveFileList.append(fileName)
    with open(fileName, 'w', encoding="utf-8") as f:
        f.writelines(bodyTag)

    sumSize = sumSize + 1
print("-------------------------------------")

save_pdf(saveFileList)
